﻿using MiniFox.Components;
using MiniFox.Data.EntityFramework;
using MiniFox.Platform.Applications.Models;
using MiniFox.Platform.Applications.Providers;
using MiniFox.Utility;

namespace Platform.Data
{
    [AutoResolve]
    public class AdministrationProvider : EntityDataProvider<PlatformDbContext>, IAdministrationProvider
    {
        public void SetEnvironmentConfig(EnvConfig envConfig)
        {
            var sysEnv = this.CurrentDbContext.SysEnvConfig.FirstOrDefault();
            var newEnv = sysEnv == null;
            if (newEnv) sysEnv = new SysEnvConfig();
            sysEnv.ConfigData = envConfig.ToJsonString();
            sysEnv.UpdateTime = DateTime.Now;

            if (newEnv)
                this.Insert(sysEnv);
            else
                this.Update(sysEnv);
        }

        public EnvConfig GetEnvironmentConfig()
        {
            var sysEnv = this.CurrentDbContext.SysEnvConfig.FirstOrDefault();
            if (sysEnv == null) return null;

            var envConfig = sysEnv.ConfigData.ToJsonObject<EnvConfig>();
            return envConfig;
        }
    }
}
